<?php
////////////////////////////////////////////////////////////////////////////////
//   Copyright (C) ReloadCMS Development Team                                 //
//   http://reloadcms.sf.net                                                  //
//                                                                            //
//   This program is distributed in the hope that it will be useful,          //
//   but WITHOUT ANY WARRANTY, without even the implied warranty of           //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     //
//                                                                            //
//   This product released under GNU General Public License v2                //
//////////////////////////////////////////////////////////////////////////////// 


//config section

$avatars_path=DATA_PATH.'avatars/';
$avatar_h='90';
$avatar_w='90';
$avatar_size="20000";
$avatars_enabled="true";

//Avatars API functions

//function thats shows avatars requitements
function show_avatar_requirements()
{
global $avatars_path;
global $avatar_h;
global $avatar_w;
global $avatar_size;
$requirements=__('Your avatar must be less than ').$avatar_size.__(' bytes in size').', have resolution at '.$avatar_w.'x'.$avatar_h.' and have type png, jpg or gif';
return $requirements;
}


// function to show avatar for such user
function show_avatar($user)
{	
global $avatars_path;
global $avatar_h;
global $avatar_w;
global $avatars_enabled;
$info=load_user_info($user);
if ($user!="guest")  {
foreach ($info as $key=>$val)
{
if (($key=='avatar') AND ($key!=""))
	{
	$have_avatar="true";
	$result='<img src='.$avatars_path.$val.' heigh="'.$avatar_h.'" width="'.$avatar_w.'">';
	}
	
}

if ((!isset($have_avatar)) OR ($have_avatar!="true"))
	{
	$result='<img src='.$avatars_path.'noavatar.gif heigh="'.$avatar_h.'" width="'.$avatar_w.'">';
	}
}

if ($user=="guest")
{
	$result="";
}

if ($avatars_enabled!="true") {
$result="";
}

return $result;
}



// function to upload avatar 
function upload_avatar()
{
global $avatars_path;
if ((isset($_POST['upload_avatar'])) AND ($_POST['upload_avatar']=="true") AND (is_image($_FILES['avatar']['name'])))
{
$uploadfile = $avatars_path . basename($_FILES['avatar']['name']);
if ((move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadfile))) {
   show_window(__('Result'),__('Avatar filesucefully uploaded'),'center');
   return $_FILES['avatar']['name'];
} else {
show_window(__('Result'),__('Your avatar don\'t meet our requirements'),'center');
}
}
	
}


// Function that shows form to upload avatar
function avatar_upload_box()
{
global $avatar_size;
$form='<form enctype="multipart/form-data" action="" method="POST">
		<input type="hidden" name="upload_avatar" value="true">
       <input type="hidden" name="MAX_FILE_SIZE" value="'.$avatar_size.'" />
       '.__('Select avatar from your HDD').': <input name="avatar" type="file"/>
    <input type="submit" value="'.__('Upload avatar').'" />
</form>';
return $form;
}

//function for check image type
function is_image($filename)
{
$ext=strtolower(substr(strrev($filename),0,4));	
if (($ext=='gpj.') OR ($ext=='gnp.') OR ($ext=='fig.') OR ($ext=='gepj'))
{
	return true;
}
else 
{
	show_window(__('Error'),__('Your avatar is not an image or is corrupted'));
	return false;
}
}

//function for update avatar for some user
function update_avatar($user,$avatar_name,$avatar_type="local")
{
 user_change_field($user, 'avatar', $avatar_name);
 user_change_field($user, 'avatar_type', $avatar_type);
}

?>
